package com.frank.code_notes.C01_array;


import cn.hutool.core.lang.Console;

/**
 * @Description 35. 搜索插入位置
 * @Author zhoujr
 * @Data 2024/11/24 19:29
 */
public class L035SearchInsertLocation {

    /**
     * @Description [left, right)
     */
    public static int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length, middle = 0;
        while (left < right) {
            middle = left + ((right - left) >> 1);
            if (nums[middle] > target) {
                right = middle;
            } else if (nums[middle] < target) {
                left = middle + 1;
            } else {
                return middle;
            }
        }
        // 目标值在数组所有元素之后的情况 [left, right)，因为是右开区间，所以 return right
        return right;
    }


    public static void main(String[] args) {
        Console.log(searchInsert(new int[]{1, 3, 5, 6}, 2));
    }

}
